package com.hu.tool.controller.fileUploadController;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@RequestMapping("/file")
@RestController
public class fileUploadController {
    @Value("${quickstart.uploadPath}")
    private String relaPath;

    /**
     * 多文件上传
     * @param files
     * @param fileName
     */
    @PostMapping("/upload")
    public void foreFileUpload(List<MultipartFile> files, String fileName) {
        List<MultipartFile> fieList = files;
        AtomicReference<File> newFile = new AtomicReference<>();
        String separator = System.getProperty("file.separator");
        fieList.forEach(m -> {
            String originalFilename = m.getOriginalFilename();
            String path = relaPath + separator + fileName;
            File file = new File(path);
            if(!file.exists()){
                file.mkdir();
            }
            path = path+separator+originalFilename;
            try {
                newFile.set(new File(path));
                m.transferTo(newFile.get());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 根据生成二维码
     */
    @GetMapping("/qrcode")
    public void generQRCode(String url,HttpServletResponse response){
        // 定义要生成二维码的基本参数
        int width = 300;
        int height = 300;
        String type = "png";
        String content = url; //要跳转的网页
        // 定义二维码的配置，使用HashMap
        HashMap<EncodeHintType, Object> hints = new HashMap();
        // 字符集，内容使用的编码
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 容错等级，H、L、M、Q
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        // 边距，二维码距离边的空白宽度
        hints.put(EncodeHintType.MARGIN, 2);
        try {
            // 生成二维码对象，传入参数：内容、码的类型、宽高、配置
            BitMatrix bitMatrix =  new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            // 定义一个路径对象,生成图片的路径
            response.setContentType("image/jpg");
            response.setHeader("Pragma",  "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            OutputStream stream = response.getOutputStream();
//            Path file = new File("D:\\IDEA背景图片\\a.jpg").toPath();
            // 生成二维码，传入二维码对象、生成图片的格式、生成的路径
            MatrixToImageWriter.writeToStream(bitMatrix, type, stream);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
